#include "./modbus/bsp_modbus.h"
#include "eeprom.h"

uint16_t holdreg[100];

stc_modbus stcModbus; //Modbus结构体
//Modbus初始化
void ModbusInit(void)
{   uint8_t buff[200];
	// buff清零
    memset(buff,0,200);
    //从EEPROM读取数据
		eeprom_read(10, buff, 200,1);
    //buff数据转换成uint16_t数组
    for(int i=0;i<sizeof(buff)/2;i++)
    {
        holdreg[i] = (buff[i*2]<<8) | buff[i*2+1];
    }

    /*
     ***************************************************************************
     * 初始化硬件
     * 例如上图使用串口与485芯片相连，则此步骤主要为串口初始化
     * 主要流程为串口时钟初始化、串口配置、串口中断配置等
     * 由于不同项目使用的单片机、硬件外设及连接不同，所以这里不对硬件层初始化进行举例
     ***************************************************************************
     */

    
}
uint8_t data_init(stc_modbus stcModbus)
{		
		int i;
    //初始化Modbus结构体
    stcModbus.address    =   0x00; //设备地址
    stcModbus.function    =   0x00; //功能码
    stcModbus.reg_addr    =   0x0000; //寄存器地址或首地址
    stcModbus.reg_num    =   0x0000; //读取寄存器数量
    stcModbus.byte_num    =   0x00; //写入数据字节数
    for (i=0;i<MODBUS_DATA_LEN;i++)
    {
        //初始化写入数据
        //此处初始化为0，实际使用时根据需要初始化为其他值
    stcModbus.data[i]    =   0x00; //写入数据
    }
    stcModbus.frame_len    =   0x00; //帧长度
    for (i=0;i<MODBUS_FRAME_LEN;i++)
    {

        //初始化帧缓存  
        //此处初始化为0，实际使用时根据需要初始化为其他值
        stcModbus.frame[i]    =   0x00;
    }
    stcModbus.crc    =   0x0000; //CRC校验码
}





// mobus客户端打包数据
//打包数据,将pstcModbus结构体内相关元素根据不同功能码帧结构存入发送帧缓存，并加CRC校验
//函数运行完毕后可根据pstcModbus.frame_len直接调用串口发送函数发送指定字节数的帧数据
uint8_t C_ModbusPackData(stc_modbus *pstcModbus)
{
    uint16_t i   =   0;
    uint16_t crc    =   0;
    //将地址、功能码、操作寄存器地址或首地址存入帧，任何功能码此部分相同
    pstcModbus->frame[0]    =   pstcModbus->address;
    pstcModbus->frame[1]    =   pstcModbus->function;
    pstcModbus->frame[2]    =   (pstcModbus->reg_addr >> 8) & 0xFF;
    pstcModbus->frame[3]    =   pstcModbus->reg_addr & 0xFF;
    //判断功能码，并根据不同功能码进行处理
    switch (pstcModbus->function) {
        //读线圈/离散输入/保持寄存器/输入寄存器帧格式在寄存器地址或首地址后均是读取数量、CRC,故可以将case条件合并
        case READ_COIL_REG:
        case READ_DIS_INPUT_REG:
        case READ_HOLD_REG:
        case READ_INPUT_REG:                       
            //存放读取数量
            pstcModbus->frame[4]    =   (pstcModbus->reg_num >> 8) & 0xFF;
            pstcModbus->frame[5]    =   pstcModbus->reg_num & 0xFF;
            //CRC计算
            crc    =   CRC16table(pstcModbus->frame, 6);
            //存放CRC校验码
            pstcModbus->frame[6]    =   (crc >> 8) & 0xFF;
            pstcModbus->frame[7]    =   crc & 0xFF;
            //帧长度
            pstcModbus->frame_len    =   8;
            break;
        //写单个寄存器线圈/保持寄存器帧格式在寄存器地址后均是写入数据、CRC,故可以将case条件合并
        case WRITE_SIN_COIL_REG:
        case WRITE_SIN_HOLD_REG:
            //存入要写入寄存器的数据
            pstcModbus->frame[4]    =   pstcModbus->data[0];
            pstcModbus->frame[5]    =   pstcModbus->data[1];
            //CRC计算
            crc    =   CRC16table(pstcModbus->frame, 6);
            //存放CRC校验码
            pstcModbus->frame[6]    =   (crc >> 8) & 0xFF;
            pstcModbus->frame[7]    =   crc & 0xFF;
            //帧长度
            pstcModbus->frame_len    =   8;
            break;
        //写多个线圈/保持寄存器帧格式在寄存器地址后均是写入寄存器数量、写入数据、CRC,故可以将case条件合并
        case WRITE_MULT_COIL_REG:
        case WRITE_MULT_HOLD_REG:
            //存放写入寄存器数量
            pstcModbus->frame[4]    =   (pstcModbus->reg_num >> 8) & 0xFF;
            pstcModbus->frame[5]    =   pstcModbus->reg_num & 0xFF;
            //存放写入数据字节数
            pstcModbus->frame[6]    =   pstcModbus->byte_num;
            //存放写入数据
            for(i=0;i<pstcModbus->byte_num;i++)
                pstcModbus->frame[7+i]    =   pstcModbus->data[i];
            //CRC计算
            crc    =   CRC16table(pstcModbus->frame, pstcModbus->byte_num+7);
            //存放CRC校验码
            pstcModbus->frame[pstcModbus->byte_num+7]    =   (crc >> 8) & 0xFF;
            pstcModbus->frame[pstcModbus->byte_num+8]    =   crc & 0xFF;
            //帧长度
            pstcModbus->frame_len    =   pstcModbus->byte_num+9;
            break;
        default:
            break;
    }
    return M_SUCCESS;
}

//modbus客户端解析数据
//解析数据，将帧缓存中的数据解析到pstcModbus结构体相关元素中
//帧缓存数据可以来自串口接收
//此函数运行完毕后可根据pstcModbus结构体内的功能码读取结构体内有用的数据
uint8_t C_ModbusParseData(stc_modbus *pstcModbus)
{
    uint8_t sta    =   M_ERROR;  
    uint16_t i   =   0; 
    uint16_t crc    =   0;
    //将帧缓存中不同功能码共有的元素解析到pstcModbus结构体对应元素：设备地址、功能码
    pstcModbus->address    =   pstcModbus->frame[0];
    pstcModbus->function   =   pstcModbus->frame[1];
    //判断功能码，并根据不同功能码进行处理
    switch (pstcModbus->function) {
    	//读线圈/离散输入/保持寄存器/输入寄存器帧格式在功能码后均是返回的读取字节数量、返回的读取数据、CRC,故可以将case条件合并
        case READ_COIL_REG:
        case READ_DIS_INPUT_REG:
        case READ_HOLD_REG:
        case READ_INPUT_REG:
       		//存放接收帧中的返回数据域字节数量
            pstcModbus->byte_num    =   pstcModbus->frame[2];
            //将接收帧中的返回数据域数据存入 pstcModbus->data[i]
            for(i=0;i<pstcModbus->byte_num;i++)
                pstcModbus->data[i]    =   pstcModbus->frame[3+i];
            //根据有原始帧数据进行CRC计算并与接收的CRC进行验证
            crc    =   ((pstcModbus->frame[pstcModbus->byte_num+3]&0x00FF)  <<8);
            crc    |=  (pstcModbus->frame[pstcModbus->byte_num+4]&0x00FF);
            pstcModbus->frame_len    =   pstcModbus->byte_num;
            if(crc == CRC16table(pstcModbus->frame, pstcModbus->byte_num+3))
                sta    =   M_SUCCESS;
            break;
        //写单个寄存器线圈/保持寄存器帧格式在功能码后均是写入寄存器地址、写入的数据、CRC,故可以将case条件合并
        case WRITE_SIN_COIL_REG:
        case WRITE_SIN_HOLD_REG:
            //存放接收帧中的寄存器地址、数据域数据
            pstcModbus->address    =   ((pstcModbus->frame[2]&0x00FF) <<8);
            pstcModbus->address    |=  (pstcModbus->frame[3]&0x00FF);
            pstcModbus->data[0]    =   pstcModbus->frame[4];
            pstcModbus->data[1]    =   pstcModbus->frame[5];
            pstcModbus->frame_len    =   2;
            //根据有原始帧数据进行CRC计算并与接收的CRC进行验证
            crc    =   ((pstcModbus->frame[6]&0x00FF)  <<8);
            crc    |=  (pstcModbus->frame[7]&0x00FF);
            if(crc == CRC16table(pstcModbus->frame, 6))
                sta    =   M_SUCCESS;
            break;
        //写多个线圈/保持寄存器帧格式在功能码后均是写入寄存器地址、操作的寄存器数量、CRC,故可以将case条件合并
        case WRITE_MULT_COIL_REG:
        case WRITE_MULT_HOLD_REG:
            //存放接收帧中的寄存器地址、操作的寄存器数量
            pstcModbus->address    =   ((pstcModbus->frame[2]&0x00FF) <<8);
            pstcModbus->address    |=  (pstcModbus->frame[3]&0x00FF);
            pstcModbus->reg_num    =   ((pstcModbus->frame[4]&0x00FF) <<8);
            pstcModbus->reg_num    |=  (pstcModbus->frame[5]&0x00FF);
            //根据有原始帧数据进行CRC计算并与接收的CRC进行验证
            crc    =   ((pstcModbus->frame[6]&0x00FF)  <<8);
            crc    |=  (pstcModbus->frame[7]&0x00FF);
            if(crc == CRC16table(pstcModbus->frame, 6))
                sta    =   M_SUCCESS;
            break;
        default:
            break;
    }
    return sta;
}

// modbus服务器解析数据
//解析数据，将帧缓存中的数据解析到pstcModbus结构体相关元素中 
//帧缓存数据可以来自串口接收
//此函数运行完毕后可根据pstcModbus结构体内的功能码读取结构体内有用的数据
uint8_t S_ModbusParseData(stc_modbus *pstcModbus)
{
    uint8_t sta    =   M_ERROR;  
    uint16_t i   =   0;
    uint16_t crc    =   0;
    //将帧缓存中不同功能码共有的元素解析到pstcModbus结构体对应元素：设备地址、功能码
    pstcModbus->address    =   pstcModbus->frame[0];
    pstcModbus->function   =   pstcModbus->frame[1];
    if (pstcModbus->address == MASTER_ADDRESS) 
    {
    //判断功能码，并根据不同功能码进行处理
    switch (pstcModbus->function) {
    	//读线圈/离散输入/保持寄存器/输入寄存器帧格式在功能码后均是读取的地址、读取的数量、CRC,故可以将case条件合并
        case READ_COIL_REG:     //01
        case READ_DIS_INPUT_REG://02
        case READ_HOLD_REG:     //03
        case READ_INPUT_REG:    //04
       		//存放读取的地址
            pstcModbus->reg_addr    =  (( pstcModbus->frame[2])<<8)+(pstcModbus->frame[3]);
            //读取的寄存器数量
            pstcModbus->reg_num    =  (( pstcModbus->frame[4])<<8)+(pstcModbus->frame[5]);
            //根据有原始帧数据进行CRC计算并与接收的CRC进行验证
            crc    =   ((pstcModbus->frame[7]&0x00FF)  <<8);
            crc    |=  (pstcModbus->frame[6]&0x00FF);
            if(crc == CRC16table(pstcModbus->frame, 6))
                sta    =   M_SUCCESS;
						for(i=0;i<(pstcModbus->reg_num)*2;i+=2) 
						{
							pstcModbus->data[i] =holdreg[pstcModbus->reg_addr+i/2]>>8;
							pstcModbus->data[i+1] =holdreg[pstcModbus->reg_addr+i/2]&0x00ff;
                            pstcModbus->byte_num = (pstcModbus->reg_num)*2;
						}

            break;
         //写单个寄存器线圈/保持寄存器帧格式在功能码后均是写入寄存器地址、写入的数据、CRC,故可以将case条件合并
        case WRITE_SIN_COIL_REG:
        case WRITE_SIN_HOLD_REG:
            //存放接收帧中的寄存器地址、数据域数据
            pstcModbus->reg_addr    =   ((pstcModbus->frame[2]&0x00FF) <<8);
            pstcModbus->reg_addr    |=  (pstcModbus->frame[3]&0x00FF);
            pstcModbus->data[0]    =   pstcModbus->frame[4];
            pstcModbus->data[1]    =   pstcModbus->frame[5];
            pstcModbus->byte_num    =   2;
            //根据有原始帧数据进行CRC计算并与接收的CRC进行验证
            crc    =   ((pstcModbus->frame[7]&0x00FF)  <<8);
            crc    |=  (pstcModbus->frame[6]&0x00FF);
            if(crc == CRC16table(pstcModbus->frame, 6))
                sta    =   M_SUCCESS;
				holdreg[pstcModbus->reg_addr]=(pstcModbus->data[0]<<8)+pstcModbus->data[1];

            break;
        //写多个线圈/保持寄存器帧格式在功能码后均是写入的地址、写入的数量、CRC,故可以将case条件合并
        case WRITE_MULT_COIL_REG:
        case WRITE_MULT_HOLD_REG:
            //存放接收帧中的寄存器地址、操作的寄存器数量
            pstcModbus->reg_addr    =   ((pstcModbus->frame[2]&0x00FF) <<8);
            pstcModbus->reg_addr    |=  (pstcModbus->frame[3]&0x00FF);
            pstcModbus->reg_num    =   ((pstcModbus->frame[4]&0x00FF) <<8);
            pstcModbus->reg_num    |=  (pstcModbus->frame[5]&0x00FF);
            // data域数据长度
            pstcModbus->byte_num    =   pstcModbus->frame[6];
            // 获取数据域数据，写入到data数组中
            for(i=0; i<pstcModbus->byte_num; i++)
            {
                pstcModbus->data[i]    =   pstcModbus->frame[7+i];  
            }
            //根据有原始帧数据进行CRC计算并与接收的CRC进行验证
            crc    =   ((pstcModbus->frame[9+i]&0x00FF)  <<8);
            crc    |=  (pstcModbus->frame[8+i]&0x00FF);
            if(crc == CRC16table(pstcModbus->frame, 8+i))
                sta    =   M_SUCCESS;
                for(i=0;i<pstcModbus->reg_num;i++)
                {
                    holdreg[pstcModbus->reg_addr+i]=(pstcModbus->data[i*2]<<8)+pstcModbus->data[i*2+1];
                }

            break;

    }
    }
    else
    {
        // 非法地址
        sta = M_ERROR;
    }
    if (sta == M_ERROR)
        // 解析失败，清除结构体
        memset(pstcModbus, 0, sizeof(stc_modbus));
	else
{
    uint8_t buff[200];
    uint16_t holdreg_size = sizeof(holdreg) / sizeof(holdreg[0]); // 获取 holdreg 的元素数量

    // 检查 holdreg 的大小是否适合 buff 数组
    if (holdreg_size * 2 > sizeof(buff)) {
        // 处理错误，例如返回错误码或记录日志
        return M_ERROR; // 或者采取其他适当的错误处理措施
				}

    // 写入 eeprom
    // holdreg 数据转化为字节数组
    for (int i = 0; i < holdreg_size; i++) {
        buff[i * 2] = holdreg[i] >> 8;
        buff[i * 2 + 1] = holdreg[i] & 0x00FF;
				}

		eeprom_write(10, buff, 200, 1); // 如果需要写入 EEPROM，取消注释
}
	
    //pstcModbus->frame清零
    memset(pstcModbus->frame, 0, sizeof(pstcModbus->frame));
		return sta;
}


// modbus服务器生成数据
//根据pstcModbus结构体中的数据生成数据帧，存放到pstcModbus结构体中的frame数组中
//此函数运行完毕后，可将pstcModbus结构体中的frame数组发送出去
uint8_t S_ModbusMakeData(stc_modbus *pstcModbus)
{
    uint8_t sta    =   M_ERROR;
    uint16_t i   =   0;
    uint16_t crc    =   0;
    //将pstcModbus结构体中的元素存放到frame数组中
    pstcModbus->frame[0]    =   pstcModbus->address;
    pstcModbus->frame[1]    =   pstcModbus->function;

    if (pstcModbus->address == MASTER_ADDRESS)
    {

    
    //判断功能码，并根据不同功能码进行处理
    switch (pstcModbus->function) {
        //读线圈/离散输入/保持寄存器/输入寄存器帧格式在功能码后均数组长度、数据、CRC,故可以将case条件合并
        case READ_COIL_REG:     //01
        case READ_DIS_INPUT_REG://02
        case READ_HOLD_REG:     //03
        case READ_INPUT_REG:    //04    
        //根据寄存器数量计算数据域数据长度
            pstcModbus->frame[2]    =  pstcModbus->byte_num;
        //将数据域数据存放到frame数组中
            for(i=0; i<pstcModbus->byte_num; i++)
            {
                pstcModbus->frame[3+i]    =   pstcModbus->data[i];  
            }
        //CRC计算
            crc    =   CRC16table(pstcModbus->frame, 3+i);
            //将CRC存放到frame数组中
            pstcModbus->frame[3+i]    =  (crc&0x00FF);
            pstcModbus->frame[4+i]    =  (crc>>8);
            //帧长度
            pstcModbus->frame_len    =   5+i;
            sta    =   M_SUCCESS;
            break;

        case WRITE_SIN_COIL_REG:     //05
        case WRITE_SIN_HOLD_REG:     //06
            //存放寄存器地址、数据域数据
            pstcModbus->frame[3]    =   (pstcModbus->reg_addr&0x00FF);   
            pstcModbus->frame[2]    =   (pstcModbus->reg_addr>>8);
            pstcModbus->frame[4]    =   pstcModbus->data[0];
            pstcModbus->frame[5]    =   pstcModbus->data[1];
            //CRC计算
            crc    =   CRC16table(pstcModbus->frame, 6);
            //将CRC存放到frame数组中
            pstcModbus->frame[6]    =   (crc&0x00FF) ;
            pstcModbus->frame[7]    =   (crc>>8);
            //帧长度
            pstcModbus->frame_len    =   8;
            sta    =   M_SUCCESS;
            break;
        //写多个线圈/保持寄存器帧格式在功能码后均是写入的地址、写入的数量、CRC,故可以将case条件合并
        case WRITE_MULT_COIL_REG:
        case WRITE_MULT_HOLD_REG:
            //存放寄存器地址、写入的数量
            pstcModbus->frame[3]    =   (pstcModbus->reg_addr&0x00FF);
            pstcModbus->frame[2]    =   (pstcModbus->reg_addr>>8);
            pstcModbus->frame[5]    =   (pstcModbus->reg_num&0x00FF);
            pstcModbus->frame[4]    =   (pstcModbus->reg_num>>8);
            //CRC计算
            crc    =   CRC16table(pstcModbus->frame, 6);
            //将CRC存放到frame数组中
            pstcModbus->frame[6]    =   (crc&0x00FF);
            pstcModbus->frame[7]    =   (crc>>8);
            //帧长度
            pstcModbus->frame_len    =   8;
            sta    =   M_SUCCESS;
            break;
            
            }
                /* code */
    }
    else
    {
        // 非法地址
        sta = M_ERROR;
        //清除结构体
        memset(pstcModbus, 0, sizeof(stc_modbus));
    }
	return sta;
}


/* CRC高字节表 */
const static uint8_t crctableh[] = {
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
	0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
	0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
	0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81,
	0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
	0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
	0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
	0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
	0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
	0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
	0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
	0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
	0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
	0x40
};
/* CRC低字节表 */
const static uint8_t crctablel[] = {
	0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4,
	0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
	0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD,
	0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
	0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7,
	0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
	0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE,
	0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
	0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2,
	0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
	0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB,
	0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
	0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91,
	0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
	0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88,
	0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
	0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80,
	0x40
};
//CRC查表函数，返回CRC-16校验码低字节在前
uint16_t CRC16table(uint8_t *ptr, uint16_t len)
{
    uint8_t crch = 0xFF;
    uint8_t crcl = 0xFF; 
    uint16_t index;
    while (len--) 
    {
        index = crcl ^ *ptr++; 
        crcl = crch ^ crctableh[index];
        crch = crctablel[index];
    }
    return ((uint16_t)(crcl | crch << 8));
}

